package com.java.action;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.alibaba.fastjson.JSON;
import com.java.domain.KsDo;
import com.java.domain.RoleDo;
import com.java.domain.UserDo;
import com.java.service.RoleService;
import com.java.service.UserService;
import com.java.utils.JsonUtils;
import com.java.utils.StrUtils;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

@Controller
@Scope("prototype")
public class PowerManageAction extends ActionSupport implements
		ModelDriven<KsDo> {

	private static final long serialVersionUID = 12L;

	@Resource
	private RoleService roleService;

	@Resource
	private UserService userService;

	public String showPowerManage() {
		return "showPowerManage";
	}

	public void update() {
		Map<String, Object> map = new HashMap<String, Object>();
		HttpServletRequest request = ServletActionContext.getRequest();
		String powerId = request.getParameter("powerId");
		String power = request.getParameter("power");
		String uid = request.getParameter("uid");
		if (StrUtils.isNotEmpty(uid) && StrUtils.isNotEmpty(power)) {
			List<RoleDo> roleList = roleService
					.quyerRole(Integer.parseInt(uid));
			if (roleList != null && roleList.size() > 0) {
				RoleDo tempRole = roleList.get(0);
				if (power.equals("3")) {
					tempRole.setRoleName("超级管理员");
				} else if (power.equals("2")) {
					tempRole.setRoleName("普通管理员");
				} else {
					tempRole.setRoleName("普通用户");
				}
				tempRole.setRolePower(Integer.parseInt(power));
				roleService.update(tempRole);
				// 修改user表中的权限
				UserDo user = userService.getById(Integer.parseInt(uid));
				user.setPower(Integer.parseInt(power));
				userService.save(user);
				map.put("success", "success");
			} else {
				RoleDo role_do = new RoleDo();
				role_do.setUid(Integer.parseInt(uid));
				role_do.setRolePower(Integer.parseInt(power));
				if (power.equals("3")) {
					role_do.setRoleName("超级管理员");
				} else if (power.equals("2")) {
					role_do.setRoleName("普通管理员");
				} else {
					role_do.setRoleName("普通用户");
				}
				roleService.save(role_do);
				map.put("success", "success");
			}
		}
		// else if (StrUtils.isNotEmpty(powerId) && StrUtils.isNotEmpty(power))
		// {
		// RoleDo tempRole = roleService.getById(Integer.parseInt(powerId));
		// if (tempRole != null) {
		// if (power.equals("3")) {
		// tempRole.setRoleName("超级管理员");
		// } else if (power.equals("2")) {
		// tempRole.setRoleName("普通管理员");
		// } else {
		// tempRole.setRoleName("普通用户");
		// }
		// tempRole.setRolePower(Integer.parseInt(power));
		// roleService.update(tempRole);
		// map.put("success", "success");
		// } else {
		// map.put("error", "error");
		// }
		// }
		else {
			map.put("error", "error");
		}
		JsonUtils.JsonTostring(map);
	}

	public void delete() {
		Map<String, Object> map = new HashMap<String, Object>();
		HttpServletRequest request = ServletActionContext.getRequest();
		String id = request.getParameter("id");
		if (StrUtils.isNotEmpty(id)) {
			roleService.delete(Integer.parseInt(id));
			map.put("success", "success");
		} else {
			map.put("error", "error");
		}
		JsonUtils.JsonTostring(map);
	}

	// 显示所有的用户的角色
	public void showRoleManageList() {
		List<RoleDo> roleList = roleService.findAll();
		if (roleList != null && roleList.size() > 0) {
			for (Iterator<RoleDo> iterator = roleList.iterator(); iterator
					.hasNext();) {
				RoleDo roleDo = iterator.next();
				UserDo userDo = userService.getById(roleDo.getUid());
				if (userDo == null) {
					iterator.remove();
					continue;
				} else {
					roleDo.setUserName(userDo.getName());
				}
				HttpServletRequest request = ServletActionContext.getRequest();
				String name = request.getParameter("name");
				if (!name.equals("undefined")) {
					UserDo u = userService.queryName(name);
					if (roleDo.getUid() != u.getId()) {
						iterator.remove();
					}
				}

			}
			System.out.println(JSON.toJSONString(roleList));
			JsonUtils.JsonTostring(roleList);
		}
	}

	public KsDo getModel() {
		return null;
	}

}
